<template>
  <Panel>
    <template #title>商品分类</template>
    <template>
      <!-- 1.0 按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" size="mini" @click="dialogVisible = true"
            >添加分类</el-button
          >
        </el-col>
      </el-row>

      <el-dialog
        title="添加分类"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <div>
          <el-form
            :model="cateForm"
            :rules="rules"
            ref="cateRef"
            label-width="100px"
            class="demo-ruleForm"
            size="mini"
          >
            <el-form-item label="分类名称" prop="name">
              <el-input v-model="cateForm.name"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
              <el-switch v-model="cateForm.state"></el-switch>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="handleClose">取 消</el-button>
          <el-button size="mini" type="primary" @click="addCate"
            >添 加</el-button
          >
        </span>
      </el-dialog>

      <!-- 2.0 表格 -->
      <el-table :data="tableData" style="width: 100%" size="mini">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column label="分类名称" width="180">
          <template v-slot="{ row }">
            <el-input v-if="row.flag" v-model="row.cateName"></el-input>
            <span v-else>{{ row.cateName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template v-slot="{ row }">
            <el-switch
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="!row.flag"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              :type="row.flag ? 'default' : 'primary'"
              size="mini"
              @click="fn(row)"
              >{{ row.flag ? '完成' : '编辑' }}</el-button
            >
            <el-button type="danger" size="mini" @click="delItem(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 3.0 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </template>
  </Panel>
</template>

<script>
import { getCates, addCates, delCates, editCates } from '@/api/goods'

export default {
  data() {
    return {
      tableData: [],
      pageSize: 5,
      currentPage: 1,
      total: 0,
      dialogVisible: false,
      cateForm: {
        name: '',
        state: true
      },
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDatas()
  },
  methods: {
    async getDatas() {
      const { data, total } = await getCates({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      console.log(data)
      // 请求回来之后的数据添加自定义的字段
      data.forEach(item => {
        item.flag = false
      })
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getDatas()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getDatas()
    },
    handleClose() {
      // 重置表单
      this.$refs.cateRef.resetFields()
      this.dialogVisible = false
    },
    async addCate() {
      const { code } = await addCates({
        cateName: this.cateForm.name,
        state: this.cateForm.state
      })
      // console.log(data)
      if (code === 0) {
        this.handleClose()
        this.getDatas()
      }
    },
    delItem(id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await delCates({ id })
          if (code === 0) {
            this.getDatas()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async fn(row) {
      if (row.flag) {
        if (row.cateName === '') return
        // 要提交请求了
        const { code } = await editCates({
          id: row.id,
          cateName: row.cateName,
          // state: Boolean(row.state)
          state: !!row.state
        })
        // console.log(data)
        if (code === 0) {
          row.flag = false
        }
      } else {
        // 编辑按钮
        row.flag = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0 40px;
}
</style>
